//输入两个整数序列，第一个序列表示栈的压入顺序，请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如，序列 {1,2,3,4,5} 是某栈
//的压栈序列，序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列，但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。 
//
// 
//
// 示例 1： 
//
// 输入：pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
//输出：true
//解释：我们可以按以下顺序执行：
//push(1), push(2), push(3), push(4), pop() -> 4,
//push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
// 
//
// 示例 2： 
//
// 输入：pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
//输出：false
//解释：1 不能在 2 之前弹出。
// 
//
// 
//
// 提示： 
//
// 
// 0 <= pushed.length == popped.length <= 1000 
// 0 <= pushed[i], popped[i] < 1000 
// pushed 是 popped 的排列。 
// 
//
// 注意：本题与主站 946 题相同：https://leetcode-cn.com/problems/validate-stack-sequences/ 
//
// Related Topics 栈 数组 模拟 👍 441 👎 0


package leetcode.editor.cn;

// [剑指 Offer 31]栈的压入、弹出序列

import java.util.Arrays;
import java.util.Stack;

public class ZhanDeYaRuDanChuXuLieLcof_JianZhiOffer31 {
    public static void main(String[] args) {
        Solution solution = new ZhanDeYaRuDanChuXuLieLcof_JianZhiOffer31().new Solution();
        solution.validateStackSequences(new int[]{1, 2, 3, 4, 5}, new int[]{4, 5, 3, 2, 1});
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            int length = pushed.length;
            Stack<Integer> stack = new Stack<>();
            int j = 0;
            for (int i = 0; i < pushed.length; i++) {
                stack.add(pushed[i]);
                while (!stack.isEmpty()&&stack.peek()==popped[j]){
                    j++;
                    stack.pop();
                }
            }
            return stack.isEmpty();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)
class Solution1 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        return validateSubSequences(pushed, popped);
    }

    public boolean validateSubSequences(int[] pushed, int[] popped) {
        if (pushed.length < 3) {
            return true;
        }
        int rootNum = pushed[0];
        int rootID = findRootID(popped, rootNum);

        if (rootID > 0 && rootID < pushed.length - 1) {
            boolean flag = isSame(pushed, popped, 0, rootID);
//                boolean flag2 = isSame(pushed,popped,)
            return flag
                    && validateSubSequences(Arrays.copyOfRange(pushed, 1, rootID + 1), Arrays.copyOfRange(popped, 0, rootID))
                    && validateSubSequences(Arrays.copyOfRange(pushed, rootID + 1, pushed.length), Arrays.copyOfRange(popped, rootID + 1, popped.length));
        } else if (rootID == 0) {
            return validateSubSequences(Arrays.copyOfRange(pushed, 1, pushed.length), Arrays.copyOfRange(popped, 1, pushed.length));
        } else if (rootID == pushed.length - 1) {
            return validateSubSequences(Arrays.copyOfRange(pushed, 1, pushed.length), Arrays.copyOfRange(popped, 0, popped.length - 1));
        } else {
            return false;
        }
    }

    public int findRootID(int[] nums, int n) {
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == n) {
                return i;
            }
        }
        return -1;
    }

    public boolean isSame(int[] nums1, int[] nums2, int i, int root) {
        int[] copy1 = Arrays.copyOfRange(nums1, i + 1, root + 1);
        int[] copy2 = Arrays.copyOfRange(nums2, i, root);
        Arrays.sort(copy1);
        Arrays.sort(copy2);
        return Arrays.equals(copy1, copy2);
    }
}
}